package io.github.viscent.mtpattern.ch10.tss.example.memoryleak;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicInteger;

@WebServlet("/pseudo-leak")
public class MemoryPseudoLeakingServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    final static ThreadLocal<AtomicInteger> TL_COUNTER = ThreadLocal.withInitial(AtomicInteger::new);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter pwr = resp.getWriter();
        pwr.write(String.valueOf(TL_COUNTER.get().getAndIncrement()));
        pwr.close();
    }

}
